package c.x.jy;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.Configuration;

import c.x.jy.code.GenUtils;
import c.x.jy.code.MySqlDb;
@SuppressWarnings("all")
public class CodeGenerator {

	// 表名   代码生成 
	private static String table = "t_member_car";
	
	private static String moduleName="membercar";
	
	private static String icon="layui-icon-app";
	private static Integer order_num=0;

	public static void main(String[] args) {

         init(table,1);
		
	 
	}
	
	/**
	 * 
	 * @param table 表名
	 * @param parentId
	 */
	private static void init(String table,Integer parentId) {
		
		Configuration config = GenUtils.getConfig();
		config.setProperty("moduleName", moduleName);
		
		MySqlDb.init(config.getString("dataUrl"), config.getString("dataUserName"), config.getString("dataPassWord"));
		String sql = "select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables "
				+ "where table_schema = (select database()) and table_name = '" + table + "'";
		
		List list = MySqlDb.mysqlSelectDb(sql);
		
		if (list == null || list.isEmpty())return;
		
		sql = "select column_name columnName, data_type dataType, column_comment comments, column_key columnKey, extra from information_schema.columns "
				+ "where table_name ='" + table + "' and table_schema = (select database()) order by ordinal_position";
		
		List list1 = MySqlDb.mysqlSelectDb(sql);
		Map table1=(Map<String, String>) list.get(0);
		GenUtils.generatorCode(table1 ,(List<Map<String, String>>) list1);
		  String className = GenUtils.tableToJava((String)table1.get("tableName"), config.getStringArray("tablePrefix")).toLowerCase();
		  String perms= config.getString( "moduleName")+":"+className.toLowerCase()+":";
		sql="INSERT INTO `sys_menu` (`parent_id`, `name`, `path`, `icon`, `order_num`, `perms`,`type`,`url`) VALUES ('"+parentId+"','"+table1.get("tableComment")+"','"+config.getString("moduleName")+"/"+className+".html','"+icon+"','"+order_num+"','','"+1+"','#!"+className+"');";
		sql+="INSERT INTO `sys_menu` (`parent_id`, `name`, `url`, `icon`, `order_num`, `perms`,`type`) VALUES ('${id}','查看'  , ''  , ' ','"+0+"','"+perms+"list,"+perms+"info" +"','"+2+"');";
		sql+="INSERT INTO `sys_menu` (`parent_id`, `name`, `url`, `icon`, `order_num`, `perms`,`type`) VALUES ('${id}','新增'  , ''  , ' ','"+0+"','"+perms+"save" +"','"+2+"');";
		sql+="INSERT INTO `sys_menu` (`parent_id`, `name`, `url`, `icon`, `order_num`, `perms`,`type`) VALUES ('${id}','修改'  , ''  , ' ','"+0+"','"+perms+"update" +"','"+2+"');";
		sql+="INSERT INTO `sys_menu` (`parent_id`, `name`, `url`, `icon`, `order_num`, `perms`,`type`) VALUES ('${id}','删除'  , ''  , ' ','"+0+"','"+perms+"delete" +"','"+2+"');";
		MySqlDb.mysqlChangeDb(sql);
		
	}

}
